﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UClaimDemageList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UClaimDemageList";
            }
        }
        #region 属性 (查询条件)


        protected string CommonFiled
        {
            get
            {
                return GetRequestValue<string>("CommonFiled", "");

            }
        }
        protected string CommonValue
        {
            get
            {
                return GetRequestValue<string>("CommonValue", "");

            }
        }

        protected string ShowAgentName
        {
            get
            {
                return GetRequestValue<string>("ShowAgentName", "");

            }
        }
        protected string UClaimType
        {
            get
            {
                return GetRequestValue<string>("UClaimType", "");
            }
        }
        protected string DownlineDateFrom
        {
            get
            {
                return GetRequestValue<string>("DownlineDateFrom", "");

            }
        }
        protected string DownlineDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(DownlineDateFrom))
                {
                    return "";                    
                }
                else
                {
                    string todate = GetRequestValue<string>("DownlineDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(DownlineDateFrom).AddDays(1).ToString("yyyy-MM-dd");
                    else
                        return todate;
                }

            }
        }

        protected string InDateFrom
        {
            get
            {
                return GetRequestValue<string>("InDateFrom", "");

            }
        }
        protected string InDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(InDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("InDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(InDateFrom).AddDays(1).ToString("yyyy-MM-dd");
                    else
                        return todate;
                }

            }
        }
        /// <summary>
        /// 只显示3个月以前发货，为交货的
        /// </summary>
        protected bool OnlyShow3MonthBefore
        {
            get
            {
                return GetRequestValue<bool>("OnlyShow3MonthBefore", false);
            }
        }

        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "TaxDate" : base.orderField;
            }
        }
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "Desc" : base.orderDirection;
            }
        }
        #endregion
        protected DataTable SUMDT = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            AddLog(Permission.查看, GetWhereStr());
        }

        protected void DataBind()
        {


            DataTableCollection dts = UClaimDemageController.GetPagination(GetWhereStr(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataSource = dts[0];
            this.rpMenu.DataBind();

            SUMDT = dts[1];
        }

        protected string GetWhereStr()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(CommonValue))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator(CommonFiled, QueryOperatorsEnum.包含, CommonValue);
                wheresql = sqlcommonFiled;
            }
            string UClaimTypeSql = "";
            if (!string.IsNullOrEmpty(UClaimType))
            {
                UClaimTypeSql = QueryOperatorsEnumHelper.ConvertCommonOperator("UClaimType", QueryOperatorsEnum.等于, UClaimType);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + UClaimTypeSql;
                }
                else
                {
                    wheresql = UClaimTypeSql;
                }
            }

            string DOWNSQL = "";
            if (!string.IsNullOrEmpty(DownlineDateFrom))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("TaxDate", QueryOperatorsEnum.范围, DownlineDateFrom + "|" + DownlineDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }


            string InToSQL = "";
            if (!string.IsNullOrEmpty(InDateFrom))
            {
                InToSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("InDate", QueryOperatorsEnum.范围, InDateFrom + "|" + InDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + InToSQL;
                }
                else
                {
                    wheresql = InToSQL;
                }
            }


            string sqlAgentName = "";
            if (!string.IsNullOrEmpty(ShowAgentName))
            {
                sqlAgentName = "'" + ShowAgentName + "'";
                sqlAgentName = sqlAgentName.Replace(",", "','");
                sqlAgentName = " agentname in (" + sqlAgentName + ")";
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlAgentName;
                }
                else
                {
                    wheresql = sqlAgentName;
                }
            }
            string onlyshow3SQL = "";
            if (OnlyShow3MonthBefore)
            {
                onlyshow3SQL = " TaxDate<=DATEADD(MONTH,-3,GETDATE())  and (Quantity-InQuantity)>0";
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + onlyshow3SQL;
                }
                else
                {
                    wheresql = onlyshow3SQL;
                }
            }

            return wheresql;
        }
     
        protected string GetAgentList()
        {
            //DataTable AgentMacStockList = UWarrantyCardController.GetAgentMacNum();

            string litemp = "<li ><a tname=\"{6}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}</a></li>";
            string listr = "";
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetAgentList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot = ShowAgentName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
               // DataRow[] drs = AgentMacStockList.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
               // string agstock = "0";
                //if (drs.Count() > 0)
                //{
                //    agstock = drs[0]["macnum"].ToString();
                //}
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());

                listr += string.Format(litemp, dr["PYAliasName"].ToString() + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check,agentname);
            }
            return listr;
        }

        protected string COldBack(object OInQuantity, object OneedbackQuantity)
        {
            decimal InQuantity = Convert.ToDecimal(OInQuantity);
            decimal needbackQuantity = Convert.ToDecimal(OneedbackQuantity);
            if (needbackQuantity <= 0)
                return "100%";

           return ((InQuantity / needbackQuantity) * 100).ToString("F2") + "%";
        }
        protected string GetUMDTValue(string filename)
        {
            if (SUMDT.Rows.Count <= 0)
            {
                return "0";
            }
            return SUMDT.Rows[0][filename].ToString();
        }
    }
}